Earthquake Crack
Data for this example comes from the USGS
First we wire up the feed for the GeoJSON into this spatial data frame:
day1quake <- geojsonio::geojson_read("https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_day.geojson",
what = "sp")
Then we load up leaflet and stick that data into a map:
library(leaflet)
m <- leaflet(day1quake) %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers()
m # Print the map
This is some code to customize markers so that we can see data points symbolized by magnitude of earthquake.
getColor <- function(day1quake) {
sapply(day1quake$mag, function(mag) {
if(mag <= 4) {
"green"
} else if(mag <= 5) {
"orange"
} else {
"red"
} })
}
icons <- awesomeIcons(
icon = 'ion-close-circled',
iconColor = 'black',
library = 'ion',
markerColor = getColor(day1quake)
)
Now we take those symbols and use them in a second map below:
leaflet(day1quake) %>% addTiles() %>%
addAwesomeMarkers(icon=icons, popup =~paste("Size:",
mag,
"<br/>",
"Place:",
place))
now we generate a data frame from the geojson feed data:
day1df <- data.frame(day1quake)
head(day1df)
From there we could throw the data into various visuals and summaries such as this histogram:
hist(day1df$mag)
rug(quantile(day1df$mag, na.rm = TRUE), col=2, lwd=2.5)
The next step here would be to wire this all up to a shiny app to work as an online web mapping application and a real time dashboard.